"""
pandas基础教程6：数据合并与拼接

【知识点解读】
pandas常用的合并方法有concat（拼接）、merge（类似SQL的连接）、join等。

【案例1】concat拼接
"""
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
print("df1：\n", df1)
print("df2：\n", df2)

# 纵向拼接
concat_df = pd.concat([df1, df2], ignore_index=True)
print("\n纵向拼接：\n", concat_df)

"""
【案例2】merge合并
"""
left = pd.DataFrame({'key': ['K0', 'K1', 'K2'], 'A': [1, 2, 3]})
right = pd.DataFrame({'key': ['K0', 'K1', 'K3'], 'B': [4, 5, 6]})
print("\nleft：\n", left)
print("right：\n", right)
merged = pd.merge(left, right, on='key', how='outer')
print("\n按key合并：\n", merged) 